<?php 

class Notifications {
	
	private $CI;
	
	public function Notifications($params = false) {
		$this->__construct($params);
	}
	
	public function __construct($params = false) {
		$this->CI =& get_instance();
	}

	public function sendNotification($timesheetid) {
		$this->CI->load->model('timesheetmodel');
		$this->CI->load->model('jobsmodel');
		$this->CI->load->model('companiesmodel');

		$notified = array();

		$sheet = $this->CI->timesheetmodel->retrieve_by_pkey($timesheetid);
		
		if ($sheet) {

			$users = $this->CI->jobsmodel->getAssignedUsers($sheet->jobid);

			$job = $this->CI->jobsmodel->retrieve_by_pkey($sheet->jobid);

			$company = $this->CI->companiesmodel->retrieve_by_pkey($sheet->companyid);

			if ($users) {

				foreach($users as $user) {
					$alertTypes = unserialize($user->notification_type);

					if ( is_array($alertTypes) && in_array('project_update', $alertTypes) && $sheet->creatorid != $user->id && !array_key_exists($user->id, $notified)) {

						if ($sheet->companyid && $sheet->jobid && !$sheet->job_costcenterid && !$sheet->activityid ) {
							$subject = "project";
							$tweet = "An update was made to the project '".$job->job_title."' for ".$company->company_name." by ".$sheet->creator.".  Find out more at ".site_url('jobs/showUpdates/'.$job->id);

							$message = "An update was made to the project '".$job->job_title."' for ".$company->company_name." by ".$sheet->creator.".\r\n\r\n".$sheet->notes."\r\n\r\nFor full details visit ".site_url('jobs/showUpdates/'.$job->id);

						} else if ($sheet->companyid && $sheet->jobid && $sheet->job_costcenterid && !$sheet->activityid ) {
							$this->CI->load->model('costcentermodel');

							$department = $this->CI->costcentermodel->retrieve_by_pkey($sheet->job_costcenterid);

							$subject = "department";
							$tweet = "An update was made to ".$department->centertitle." in '".$job->job_title."' for ".$company->company_name." by ".$sheet->creator.".  Find out more at ".site_url('jobs/showUpdates/'.$job->id);
							
							$message = "An update was made to ".$department->centertitle." in '".$job->job_title."' for ".$company->company_name." by ".$sheet->creator.".\r\n\r\n".$sheet->notes."\r\n\r\nFor full details visit ".site_url('jobs/showUpdates/'.$job->id);
						
						} else if ($sheet->companyid && $sheet->jobid && $sheet->job_costcenterid && $sheet->activityid ) {
							
							$this->CI->load->model('costcentermodel');
							$this->CI->load->model('activitymodel');
						
							$department = $this->CI->costcentermodel->retrieve_by_pkey($sheet->job_costcenterid);
							$task = $this->CI->activitymodel->retrieve_by_pkey($sheet->activityid);
							
							$subject = "task";
							$tweet = "An update was made to the task '".$task->activity_title."' in ".$department->centertitle." in '".$job->job_title."' for ".$company->company_name." by ".$sheet->creator.".  Find out more at ".site_url('jobs/showUpdates/'.$job->id);
							
							$message = "An update was made to the task '".$task->activity_title."' in ".$department->centertitle." in '".$job->job_title."' for ".$company->company_name." by ".$sheet->creator.".\r\n\r\n".$sheet->notes."\r\n\r\nFor full details visit ".site_url('jobs/showUpdates/'.$job->id);
						}
					
						//if twitter notification on send notification
						// if (0 && $user->notify_twitter && $user->twitter_username) {
						//	$this->CI->load->library('twitter', array('username' => 'epicentr', 'password' => '0r4ng3d0g'));
						//	$this->CI->twitter->updateDirect($user->twitter_username, $tweet );
						// }
						
						//if email notification on send notification
						if (0 && $user->notify_email) {
							$this->CI->load->library('email');
				
							$this->CI->email->from('alerts@getepicentr.net', 'epicentr Alerts');
							$this->CI->email->to($user->emailaddress);  
							
							$this->CI->email->subject('['.$company->account_code.'] ['.$job->job_number.'] '.ucfirst($subject).' update on epicentr');
							$this->CI->email->message("Hi ".$user->firstname.",\r\n\r\n".$message."\r\n\r\n epicentr ".base_url());
							
							$this->CI->email->send();
						}
						
						$notified[$user->id] = true;
						
					}			
				}
				
			}
		}		
		
		return true;
	}
	
	public function assignmentNotification($userid, $companyid, $jobid, $departmentid = false, $taskid = false) {
		$this->CI->load->model('usersmodel');
		$this->CI->load->model('companiesmodel');
		$this->CI->load->model('jobsmodel');
	
		$user = $this->CI->usersmodel->retrieve_by_pkey($userid);
		
		$company = $this->CI->companiesmodel->retrieve_by_pkey($companyid);
		
		$job = $this->CI->jobsmodel->retrieve_by_pkey($jobid);
		
		$alertTypes = unserialize($user->notification_type);
		
		//generate message depending on type of assignation
		
		if ($companyid && $jobid && !$departmentid && !$taskid ) {
			$subject = "project";
			$message = "You have been assigned to the project '".$job->job_title."' for ".$company->company_name.".  Find out more at ".site_url('jobs/showUpdates/'.$job->id);
		} else if ($companyid && $jobid && $departmentid && !$taskid ) {
			$this->CI->load->model('costcentermodel');
		
			$department = $this->CI->costcentermodel->retrieve_by_pkey($departmentid);
		
			$subject = "department";
			$message = "You have been assigned to ".$department->centertitle." in '".$job->job_title."' for ".$company->company_name.".  Find out more at ".site_url('jobs/view/'.$job->id);
		} else if ($companyid && $jobid && $departmentid && $taskid ) {
			$this->CI->load->model('costcentermodel');
			$this->CI->load->model('activitymodel');
		
			$department = $this->CI->costcentermodel->retrieve_by_pkey($departmentid);
			$task = $this->CI->activitymodel->retrieve_by_pkey($taskid);
			
			$subject = "task";
			$message = "You have been assigned to the task '".$task->activity_title."' in ".$department->centertitle." in '".$job->job_title."' for ".$company->company_name.".  Find out more at ".site_url('jobs/view/'.$job->id);
		}
		
		//if twitter notification on send notification
		// if ($user->notify_twitter && $user->twitter_username && in_array('assign', $alertTypes)) {
			//these details need to be specified in the config
			//$this->CI->load->library('twitter', array('username' => '', 'password' => ''));
		//	$this->CI->twitter->updateDirect($user->twitter_username, $message );
		// }
		
		//if email notification on send notification
		if ($user->notify_email && in_array('assign', $alertTypes)) {
			$this->CI->load->library('email');

			$this->CI->email->from('alerts@epicentr.ocean70.com', 'epicentr Alerts');
			$this->CI->email->to($user->emailaddress);  
			
			$this->CI->email->subject('['.$company->account_code.'] ['.$job->job_number.'] Assigned to new '.$subject.' on epicentr');
			$this->CI->email->message("Hi ".$user->firstname.",\r\n\r\n".$message."\r\n\r\n epicentr ".base_url());
			
			$this->CI->email->send();
		}
		
		return true;
	}
	
}

?>